package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        System.out.println("请输入你要删除图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUseSize();
        int index = 0;
        for (int i = 0; i < currentSize; i++) {

            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                index = i;
                break;

            }
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book =  bookList.getBook(i+1);
            bookList.setBooks(i,book);
        }

        bookList.setBooks(currentSize-1,null );
        bookList.setUseSize(currentSize-1);
        System.out.println("删除成功!");
    }
}
